package com.by.log.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.by.log.constants.LogLevel;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 数据库日志实体类
 */
@Data
@TableName("log_entries")
public class LogEntity {
    /**
     * 主键ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 服务名称
     */
    private String serviceName;

    /**
     * 日志级别
     */
    private String level;

    /**
     * 日志内容
     */
    private String message;

    /**
     * 异常信息
     */
    private String throwable;

    /**
     * 时间戳
     */
    private LocalDateTime timestamp;

    /**
     * 链路追踪ID
     */
    private String traceId;

    /**
     * 跨度ID
     */
    private String spanId;

    /**
     * 父跨度ID
     */
    private String parentSpanId;

    /**
     * 类名
     */
    private String className;

    /**
     * 方法名
     */
    private String methodName;

    /**
     * 线程名
     */
    private String threadName;

    /**
     * 扩展字段，用于AI分析
     */
    private String extendedFields;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    public static LogEntity fromLogEntry(LogEntry entry) {
        LogEntity logEntity = new LogEntity();
        logEntity.setServiceName(entry.getServiceName());
        logEntity.setLevel(entry.getLevel() != null ? entry.getLevel().name() : LogLevel.INFO.name());
        logEntity.setMessage(entry.getMessage());
        logEntity.setThrowable(entry.getThrowable());
        logEntity.setTimestamp(entry.getTimestamp());
        logEntity.setTraceId(entry.getTraceId());
        logEntity.setSpanId(entry.getSpanId());
        logEntity.setParentSpanId(entry.getParentSpanId());
        logEntity.setClassName(entry.getClassName());
        logEntity.setMethodName(entry.getMethodName());
        logEntity.setThreadName(entry.getThreadName());
        // 这里可以序列化extendedFields，简化处理直接转为字符串
        logEntity.setExtendedFields(entry.getExtendedFields() != null ? entry.getExtendedFields().toString() : null);
        logEntity.setCreateTime(LocalDateTime.now());
        logEntity.setUpdateTime(LocalDateTime.now());
        return logEntity;
    }
    
    // 添加setter方法以解决编译错误
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    
    public void setLevel(String level) {
        this.level = level;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public void setThrowable(String throwable) {
        this.throwable = throwable;
    }
    
    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }
    
    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
    
    public void setSpanId(String spanId) {
        this.spanId = spanId;
    }
    
    public void setParentSpanId(String parentSpanId) {
        this.parentSpanId = parentSpanId;
    }
    
    public void setClassName(String className) {
        this.className = className;
    }
    
    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }
    
    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }
    
    public void setExtendedFields(String extendedFields) {
        this.extendedFields = extendedFields;
    }
    
    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }
    
    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }
}